package com.gzz.boot.sequence;

import com.google.common.util.concurrent.ThreadFactoryBuilder;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadFactory;

public class IDTaskManager {

    private static int counter = 0;

    @Autowired
    private RedisSegmentIDService idService;

    public static synchronized void addCounter(String threaName, int value) {
        counter += value;
        System.out.println("Updated Counter: " + counter);
    }

    public void start(int threadCount) throws InterruptedException{
        counter = 0;
        System.out.println("START THREAD COUNT: " + threadCount);
        ThreadFactory namedThreadFactory = new ThreadFactoryBuilder().setNameFormat("id-task-thread-%d").build();
        ExecutorService executor = Executors.newFixedThreadPool(threadCount, namedThreadFactory);
        int i = 0;
        while (i < threadCount) {
            IDTask idTask = new IDTask(idService);
            executor.submit(idTask);
            i++;
        }
    }
}
